package apiv1

import (
	"github.com/gin-gonic/gin"
	"go-shop-admin/internal/service"
	"go-shop-admin/pkg/e"
	"go-shop-admin/pkg/serializer"
	"go-shop-admin/pkg/zydhlog"
	"net/http"
)

//管理员控制器

// AuthAdd 新增管理员
func AuthAdd(c *gin.Context) {
	var authService *service.AuthService
	//authService := service.AuthService{}
	if err := c.ShouldBind(&authService); err != nil {
		//返回验证器验证错误信息
		c.JSON(http.StatusBadRequest, Validators(err))
		//写入日志
		return
	}
	res := authService.Create(c.Request.Context())
	c.JSON(http.StatusOK, res)
}

// AuthShow 显示
func AuthShow(c *gin.Context) {
	var authService *service.AuthService
	id, _, err := GetRestfulApiId(c.Param("id"), 1)
	if err != nil {
		c.JSON(http.StatusBadRequest, serializer.NewResponse(e.InvalidParams, err.Error(), e.TipInvalidParams, nil))
		return
	}

	res := authService.Show(c.Request.Context(), id)
	c.JSON(http.StatusOK, res)
}

// AuthUpdate 修改
func AuthUpdate(c *gin.Context) {
	var authService *service.AuthService
	if err := c.ShouldBind(&authService); err != nil {
		//返回验证器验证错误信息
		c.JSON(http.StatusOK, Validators(err))
		zydhlog.Info("this is validator err", err)
		return
	}

	id, _, err := GetRestfulApiId(c.Param("id"), 1)
	if err != nil {
		c.JSON(http.StatusBadRequest, serializer.NewResponse(e.InvalidParams, err.Error(), e.TipInvalidParams, nil))
		return
	}

	res := authService.Update(c.Request.Context(), id)
	c.JSON(http.StatusOK, res)
}

// AuthDelete 删除
func AuthDelete(c *gin.Context) {

	_, ids, err := GetRestfulApiId(c.Param("ids"), 2)
	if err != nil {
		c.JSON(http.StatusBadRequest, serializer.NewResponse(e.InvalidParams, err.Error(), e.TipInvalidParams, nil))
		return
	}

	var authService *service.AuthService
	res := authService.Delete(c.Request.Context(), ids)
	c.JSON(http.StatusOK, res)
}
